/**
 * 
 */
package com.freecsim.sas.core.services;

import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.freecsim.sas.core.ServiceManager;
import com.freecsim.sas.core.exception.ServiceIDExistsException;
import com.freecsim.sas.core.exception.ServiceNotFoundException;
import com.freecsim.sas.core.exception.ServiceStartException;
import com.freecsim.sas.core.services.IService.Status;

/**
 * @author georgewu
 *
 */
public class ServiceManagerImp implements ServiceManager{

	private static Logger logger = Logger.getLogger(ServiceManagerImp.class);
	/**
	 * 服务列表
	 */
	private Map<String, IService> services; 
	
	/**
	 * 构造方法
	 */
	public ServiceManagerImp(){
		services = new HashMap<String, IService>();
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.freecsim.server.framework.core.ServiceManager#closeServices()
	 */
	public void closeServices() {
		List<IService> list = new LinkedList<IService>();
		list.addAll(services.values());
		Collections.sort(list, new Comparator<IService>() {
			/* (non-Javadoc)
			 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
			 */
			public int compare(IService o1, IService o2) {
				return o1.getStartOn() < o2.getStartOn() ? 1 : 0;
			}
		});
		
		if(list.size() == 0){
			logger.warn("The list of services is null, no one service will be closed.");
		}
		
		for(IService s : list){
			logger.info("Stoping service " + s.getName());
			try{
				s.shutdown();
				logger.info("The service[" + s.getName() + "] is shutdown.");
			}catch(Exception ex){
				logger.info("Stop service["+ s.getName() +"] exception:" + s.getName());
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.freecsim.server.framework.core.ServiceManager#getService(java.lang.String)
	 */
	public IService getService(String name) throws ServiceNotFoundException {
		if(services.containsKey(name)){
			return services.get(name);
		}else{
			throw new ServiceNotFoundException("ID为" + name + "的服务没有找到");
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.freecsim.server.framework.core.ServiceManager#registerService(com.freecsim.server.framework.core.services.IService)
	 */
	public void registerService(IService service) throws ServiceIDExistsException {
		if(services.containsKey(service.getName())){
			throw new ServiceIDExistsException("名称为:" + service.getName() +" 的服务已经存在");
		}
		services.put(service.getName(), service);
	}

	/*
	 * (non-Javadoc)
	 * @see com.freecsim.server.framework.core.ServiceManager#startServices()
	 */
	public void startServices() throws ServiceStartException {
		
		List<IService> list = new LinkedList<IService>();
		list.addAll(services.values());
		Collections.sort(list, new Comparator<IService>() {
			/* (non-Javadoc)
			 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
			 */
			public int compare(IService o1, IService o2) {
				return o1.getStartOn() > o2.getStartOn() ? 1 : 0;
			}
		});
		
		if(list.size() == 0){
			logger.warn("The list of services is null, no one service will be startup.");
		}
		
		for(IService s : list){
			try{
				logger.info("Starting service " + s.getName());
				s.setStatus(Status.STATUS_STARTING);
				s.start();
				s.setStatus(Status.STATUS_RUNNING);
			}catch(Exception e){
				throw new ServiceStartException(e);
			}
		}
	}
}
